home *** CD-ROM | disk | FTP | other *** search
/ ShareWare OnLine 2 / ShareWare OnLine Volume 2 (CMS Software)(1993).iso / games2 / rotise12.zip / RDB_PLAY.C < prev    next >
C/C++ Source or Header  |  1992-04-03  |  9KB  |  372 lines

  1. /* RDB_PLAYR - RDB player creating subroutines */
  2.  
  3. #ifdef    __TURBOC__
  4. #include <stdlib.h>
  5. #endif
  6.  
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <ctype.h>
  10.  
  11. #ifdef    __TURBOC__
  12. #include <alloc.h>
  13. #else
  14. #include <malloc.h>
  15. #endif
  16.  
  17. #include "rotise.h"
  18.  
  19. /* External data referenced */
  20. extern CMDLIST CmdTable[];                    /* cmdtable.c */
  21. extern int Cmd;                                /* rotise.c */
  22.  
  23.  
  24. /* Local data publicly available */
  25. int    SlotC;                                    /* Number of slots defined */
  26. int    SlotS;                                    /* Size of slot table */
  27. RDB_SLOT *Slottbl;                            /* Slot table */
  28.  
  29. /* Private data */
  30.  
  31.     /* Table of position/mask pairs */
  32. static struct {
  33.     char    *pos;
  34.     UWORD posmask;
  35.     char    *desc;
  36.                   } Postbl[] = {
  37.         { "P",    POS_P,    "Pitcher"                    },        /* Pitcher */
  38.         { "C",    POS_C,    "Catcher"                    },        /* Catcher */
  39.         { "1B",    POS_1B,    "First Base"                },        /* First */
  40.         { "2B",    POS_2B,    "Second Base"                },        /* Second */
  41.         { "3B",    POS_3B,    "Third Base"                },        /* Third */
  42.         { "SS",    POS_SS,    "Shortstop"                    },        /* Short */
  43.         { "OF",    POS_OF,    "Outfield"                    },        /* Outfield */
  44.         { "DH",    POS_DH,    "Designated Hitter"        },        /* DH */
  45.         { NULL,  0                }
  46.                                     };
  47.  
  48.  
  49. /* Local Prototypes */
  50. void build_player PROTO( (RDB_PLAYER *rp, int tokcnt, char *tokens[]) );
  51. BOOL verify_contract PROTO( (char *) );
  52. BOOL verify_salary PROTO( (char *) );
  53. /*
  54.     BOOL rdb_add_player( RDB_PLAYER *rp, int tokcnt, char *tokens[] )
  55.      Verify the data and then add to the player structure.
  56.  
  57.  ACCEPTS:
  58.     RDB_PLAYER *rp - player structure to fill in
  59.     int tokcnt - how many tokens on this line
  60.     char *tokens[] - array pointing to each token
  61.  
  62.  RETURNS:
  63.     BOOL <value> - TRUE if everything was okay, FALSE if things really aren't
  64. */
  65.  
  66. BOOL rdb_add_player ARGLIST( (rp, tokcnt, tokens) )
  67.     NFARG( RDB_PLAYER *rp )
  68.     NFARG( int tokcnt )
  69.     FARG( char *tokens[] )
  70. {
  71.     int    slotN;
  72.  
  73.     if ( tokcnt < (ARG_ADD_NAME+1) )
  74.     { /* minimum number of arguments for a player */
  75.         rdb_error( "Add ", CmdTable[Cmd].usage );
  76.         return FALSE;
  77.     }
  78.  
  79.     if ( !verify_salary( tokens[ARG_ADD_SAL] ) )
  80.     { /* salary doesn't pass muster, complain */
  81.         rdb_error( "Add - Incomprehensible salary: ", tokens[ARG_ADD_SAL] );
  82.         return FALSE;
  83.     }
  84.  
  85.     if ( !verify_contract( tokens[ARG_ADD_CONT] ) )
  86.     { /* unknown contract identifier */
  87.         rdb_error( "Add - Unknown contract: ", tokens[ARG_ADD_CONT] );
  88.         return FALSE;
  89.     }
  90.  
  91.     slotN = slot_find( tokens[ARG_ADD_SLOT] );
  92.     if ( slotN < 0 )
  93.     { /* slot no good */
  94.         rdb_error( "Add - Invalid Slot.", tokens[ARG_ADD_SLOT] );
  95.         return FALSE;
  96.     }
  97.  
  98.     if ( strlen( tokens[ARG_ADD_PK1] ) > sizeof( rp->pname.pn_lname )-1 )
  99.     { /* last name of key too big */
  100.         rdb_error( "Add - Player key, part 1, too long.", tokens[ARG_ADD_PK1] );
  101.         return FALSE;
  102.     }
  103.  
  104.     if ( strlen( tokens[ARG_ADD_PK2] ) > sizeof( rp->pname.pn_fname )-1 )
  105.     { /* last name of key too big */
  106.         rdb_error( "Add - Player key, part 2, too long.", tokens[ARG_ADD_PK2] );
  107.         return FALSE;
  108.     }
  109.  
  110.     /* okay, we have all the verified data ready to go into the structure */
  111.     build_player( rp, tokcnt, tokens );
  112.  
  113.     return TRUE;
  114. }
  115.  
  116. /*
  117.     BOOL build_player( RDB_PLAYER *rp, int tokcnt, char *tokens[] )
  118.      Actually put the player data into passed structure.  Should be all verified
  119.     by now. 
  120.  
  121.  ACCEPTS:
  122.     RDB_PLAYER *rp - player structure to fill in
  123.     int tokcnt - how many tokens on this line
  124.     char *tokens[] - array pointing to each token
  125.  
  126.  RETURNS:
  127.     BOOL <value> - TRUE if everything was okay, FALSE if things really aren't
  128. */
  129. void build_player ARGLIST( (new_player, tokcnt, tokens) )
  130.     NFARG(     RDB_PLAYER *new_player )
  131.     NFARG(     int tokcnt )
  132.     FARG(        char *tokens[] )
  133. {
  134.     int kk;
  135.     int slotN;
  136.  
  137.     /* first, build the players name */
  138.     for ( kk = ARG_ADD_NAME; kk < tokcnt; kk++ )
  139.     { /* run the player name together, seperated by spaces */
  140.         if ( (strlen( tokens[kk] ) + strlen( new_player->name )) > MAX_NAMESIZE )
  141.         { /* can't fit any more, complain and truncate */
  142.             rdb_error( "Build - Player name too long, truncating.", tokens[kk] );
  143.             break;
  144.         }
  145.         else if ( kk != ARG_ADD_NAME )
  146.         { /* put in a space */
  147.             strcat( new_player->name, " " );
  148.         }
  149.  
  150.         strcat( new_player->name, tokens[kk] );
  151.     }
  152.  
  153.     /* copy in strings */
  154.     strcpy( new_player->pname.pn_lname, strupr( tokens[ARG_ADD_PK1] ) );
  155.     strcpy( new_player->pname.pn_fname, strupr( tokens[ARG_ADD_PK2] ));
  156.  
  157.     slotN = slot_find( tokens[ARG_ADD_SLOT] );
  158.     new_player->slot = slotN;
  159.     if ( slotN < 0 )
  160.     {    /* Unknown slot -- give warning and continue. */
  161.         rdb_error( "Build - unknown slot: ", tokens[ARG_ADD_SLOT] );
  162.     }
  163.  
  164.     new_player->status = STATUS_ACTIVE;                    /* Show activeness */
  165.  
  166.     new_player->salary = atoi( tokens[ARG_ADD_SAL] );
  167.     switch( tolower(*tokens[ARG_ADD_CONT]) )
  168.     { /* get contract value */
  169.     case 'a':                                /* first year */
  170.         new_player->contract = CONT_1;
  171.         break;
  172.     case 'b':                                /* second year */
  173.         new_player->contract = CONT_2;
  174.         break;
  175.     case 'o':                                /* option year */
  176.         new_player->contract = CONT_OPT;
  177.         break;
  178.     default:                                    /* long term - say how many years left */
  179.         new_player->contract = CONT_YR;
  180.         new_player->contract |= ( atoi( tokens[ARG_ADD_CONT] ) << CONT_YRSH );
  181.         break;
  182.     }
  183.  
  184.     new_player->next = NULL;
  185. }
  186.  
  187. /* verify_salary - make sure the passed salary is reasonable, i.e., it's
  188. /*  all numbers and not zero. */
  189.  
  190. BOOL verify_salary ARGLIST( (salary) )
  191.     FARG( char *salary )
  192. {
  193.     int kk;
  194.     int sal_len = strlen( salary );
  195.  
  196.     for ( kk = 0; kk < sal_len; kk++ )
  197.     { /* scan the string for non-number */
  198.         if ( !isdigit( *(salary+kk) ) )
  199.         { /* oops, problem with salary */
  200.             return FALSE;
  201.         }
  202.     }
  203.  
  204.     if ( atoi( salary ) == 0 )
  205.     { /* must not be zero, okay? */
  206.         return FALSE;
  207.     }
  208.  
  209.     return TRUE;
  210. }
  211.  
  212. /* verify_contract - make sure contract is reasonable */
  213. static char *Contracts = "abo123456789";
  214.  
  215. BOOL verify_contract ARGLIST( (contract) )
  216.     FARG( char *contract )
  217. {
  218.     if ( strlen( contract ) > 1 )
  219.     { /* can't be longer than one */
  220.         return FALSE;
  221.     }
  222.  
  223.     if ( strchr( Contracts, *contract ) == NULL )
  224.     { /* not found in contract */
  225.         return FALSE;
  226.     }
  227.  
  228.     return TRUE;
  229. }
  230.  
  231. char *contract_string ARGLIST( (contract) )
  232.     FARG(        int contract     )
  233. {
  234.     char *x;
  235.  
  236.     switch( contract )
  237.     { /* get contract value */
  238.     case CONT_1:                                /* first year */
  239.         return "A";
  240.     case CONT_2:                                /* second year */
  241.         return "B";
  242.     case CONT_OPT:                                /* option year */
  243.         return "O";
  244.     default:                                    /* long term - say how many years left */
  245.         x = "x";
  246.       *x = '0' + (contract >> CONT_YRSH);
  247.         return x;
  248.     }
  249. }
  250.  
  251.  
  252. /* Return slot data given its slot number */
  253. RDB_SLOT *slot_get ARGLIST( (slotnum) )
  254.      FARG( int slotnum )
  255. {
  256.     if ( ( slotnum < 0 ) || ( slotnum >= SlotC ) )
  257.     {    /* Slot number is out of range */
  258.         return ( ( RDB_SLOT * ) NULL );
  259.     }
  260.  
  261.     return( &Slottbl[slotnum] );
  262. }
  263.  
  264.  
  265. /* Return slot number given its slot name */
  266. int slot_find ARGLIST( (slotname) )
  267.      FARG( char *slotname )
  268. {
  269.     int    slotnum;
  270.  
  271.     /* Make sure name is not too long */
  272.     if ( strlen( slotname ) > MAX_SLOT )
  273.         return ( -1 );
  274.  
  275.    /* Go through the table */
  276.     for( slotnum = 0; slotnum < SlotC; ++slotnum )
  277.         if ( stricmp( slotname, Slottbl[slotnum].slot ) == 0 )
  278.             return ( slotnum );
  279.  
  280.     /* Didn't find one */
  281.     return ( -1 );
  282. }
  283.  
  284.  
  285.  
  286. /* Add a new slot */
  287. BOOL slot_add ARGLIST( (slotP) )
  288.      FARG( RDB_SLOT *slotP )
  289. {
  290.     int tblsize;                                    /* Size required for new table */
  291.  
  292.  
  293.     /* Make sure this slot name has not already been used */
  294.     if ( slot_find( slotP->slot ) >= 0 )
  295.         return ( FALSE );
  296.  
  297.     /* Add an entry in the table */
  298.     if ( SlotC == SlotS )
  299.     {    /* Must reallocate a table */
  300.         tblsize = (SlotS+10) * sizeof( RDB_SLOT );
  301.         if ( SlotS == 0 )
  302.             Slottbl = (RDB_SLOT *)malloc( tblsize );
  303.         else
  304.             Slottbl = (RDB_SLOT *)realloc( Slottbl, tblsize );
  305.         if ( Slottbl == NULL )
  306.         {
  307.             rdb_error( "Can't allocate slots table.", "" );
  308.             return ( FALSE );
  309.         }
  310.         SlotS += 10;
  311.     }
  312.  
  313.     /* Fill in new entry */
  314.     strcpy( Slottbl[SlotC].slot, slotP->slot );
  315.     Slottbl[SlotC].pos = slotP->pos;
  316.  
  317.     return (SlotC++);
  318. }
  319.  
  320.  
  321. /* Convert position name to position mask */
  322. UWORD pos_to_mask ARGLIST( (pos) )
  323.      FARG( char *pos )
  324. {
  325.     int    i;
  326.  
  327.     for ( i = 0; Postbl[i].pos != NULL; ++i )
  328.     {    /* Lookup the position */
  329.         if ( stricmp( pos, Postbl[i].pos ) == 0 )
  330.             return ( Postbl[i].posmask );
  331.     }
  332.  
  333.     /* Not found. */
  334.     return ( 0 );
  335. }
  336.  
  337.  
  338. /* Convert position mask to position name */
  339. char *mask_to_pos ARGLIST( (posmask) )
  340.      FARG( UWORD posmask )
  341. {
  342.     int    i;
  343.  
  344.     for ( i = 0; Postbl[i].pos != NULL; ++i )
  345.     {    /* Lookup the mask */
  346.         if ( posmask == Postbl[i].posmask )
  347.             return ( Postbl[i].pos );
  348.     }
  349.  
  350.     /* Not found. */
  351.     return ( (char *)NULL );
  352. }
  353.  
  354.  
  355. /* Convert position mask to position description */
  356. char *mask_to_posdesc ARGLIST( (posmask) )
  357.      FARG( UWORD posmask )
  358. {
  359.     int    i;
  360.  
  361.     for ( i = 0; Postbl[i].pos != NULL; ++i )
  362.     {    /* Lookup the mask */
  363.         if ( posmask == Postbl[i].posmask )
  364.             return ( Postbl[i].desc );
  365.     }
  366.  
  367.     /* Not found. */
  368.     return ( (char *)NULL );
  369. }
  370.  
  371.  
  372.